﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;

namespace BtLabelBLL
{
    public class PowerBLL
    {
        /// <summary>
        /// 电源USB visa描述符
        /// </summary>
        public string _device_INSTR;
        /// <summary>
        /// 通道日志控件
        /// </summary>
        public RichTextBox _richTextBoxInfo = null;
        /// <summary>
        /// 发送命令
        /// </summary>
        /// <param name="viSession">电源对象</param>
        /// <param name="order">指令</param>
        /// <returns></returns>
        private bool Send(int viSession, string order)
        {
            try
            {
                UiBLL.RichTextBoxAppendText(_richTextBoxInfo, Brushes.SteelBlue, DateTime.Now.ToString("HH:mm:ss.fff") + "\r\n"+ order);
                int result = Visa32BLL.viPrintf(viSession, order);

                if (result < 0)
                    return false;
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 设置电源电压输出
        /// </summary>
        /// <param name="volvalue">电压</param>
        /// <returns></returns>
        public bool SetVol(string volvalue)
        {
            try
            {
                int viDefRm = 0;
                int viSession = 0;
                Visa32BLL.viOpenDefaultRM(out viDefRm);
                Visa32BLL.viOpen(viDefRm, _device_INSTR, 0, 5000, out viSession);
                string order = "";
                //设置电压
                order = "VOLT " + volvalue +"\n";
                if (Send(viSession, order) == false)
                    return false;
                //输出电压
                order = ":OUTP ON\n";
                if (Send(viSession, order) == false)
                    return false;
                Visa32BLL.viClose(viSession);
                Visa32BLL.viClose(viDefRm);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 关闭电源输出
        /// </summary>
        /// <returns></returns>
        public bool Close()
        {
            try
            {
                int viDefRm;
                int viSession;
                Visa32BLL.viOpenDefaultRM(out viDefRm);
                Visa32BLL.viOpen(viDefRm, _device_INSTR, 0, 5000, out viSession);
                //关闭输出
                string order = ":OUTP OFF\n";
                if (Send(viSession, order) == false)
                    return false;
                Visa32BLL.viClose(viSession);
                Visa32BLL.viClose(viDefRm);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
